#pragma once
#include<iostream>
#include<string>
#include<vector>
#include"Oj_Model1.hpp"
#include<ctemplate/template.h>

namespace view_ns
{
    using namespace model_ns;
    using namespace ctemplate;

    const static std::string comm_path = "./Template_html/";
    class View//帮助我们完成网页的渲染工作
    {
        public:
            View(){}
            ~View(){}
        public:
            void AllRenderHtml(std::vector<Question*> question_arr,std::string* html)
            {
                //1.创建字典
                TemplateDictionary root("AllQuestionList");

                //2.往字典中插入数据
                for(int i = 0;i < question_arr.size();++i)
                {
                    TemplateDictionary* sub = root.AddSectionDictionary("OneQuestionList");
                    sub->SetValue("number",question_arr[i]->number);
                    sub->SetValue("title",question_arr[i]->title);
                    sub->SetValue("star",question_arr[i]->level);
                }

                //3.加载模板html
                Template* tpl = Template::GetTemplate(comm_path+"All_Question.html",DO_NOT_STRIP);

                //4.渲染模板
                tpl->Expand(html,&root);

                for(auto& ptr:question_arr)
                    delete ptr;
            }

            void OneRenderHtml(Question* question,std::string* html)
            {
                LOG(INFO) << "开始渲染网页了..." << "\n";
                //1.创建字典
                TemplateDictionary root("OneQuestion");
                
                //2.设置变量的值
                root.SetValue("desc",question->desc);
                root.SetValue("star",question->level);
                root.SetValue("pre_code",question->header);
                root.SetValue("number",question->number);
                root.SetValue("title",question->title);

                //3.加载模板文件
                Template* tpl = Template::GetTemplate(comm_path+"One_Question.html",DO_NOT_STRIP);

                //4.将模板文件渲染
                tpl->Expand(html,&root);

                delete question;
            }
    };
}
